package com.shangcheng.dubbo.api;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.exception.BusinessException;
import com.shangcheng.dubbo.mappers.PmsProductMapper;
import com.shangcheng.model.domain.PmsProduct;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsProductApiImpl implements PmsProductApi {
    @Autowired
    private PmsProductMapper productMapper;

    @Override
    public IPage<PmsProduct> listByPage(Integer brandId, String keyword, Integer pageNum, Integer pageSize, String productSn, Integer productCategoryId, Integer verifyStatus, Integer publishStatus) {
        IPage<PmsProduct> page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(brandId), PmsProduct::getBrandId, brandId);
        wrapper.eq(StrUtil.isNotBlank(productSn), PmsProduct::getProductSn, productSn);
        wrapper.eq(ObjectUtil.isNotNull(productCategoryId), PmsProduct::getProductCategoryId, productCategoryId);
        wrapper.eq(ObjectUtil.isNotNull(verifyStatus), PmsProduct::getVerifyStatus, verifyStatus);
        wrapper.eq(ObjectUtil.isNotNull(publishStatus), PmsProduct::getPublishStatus, publishStatus);
        wrapper.eq(PmsProduct::getDeleteStatus, 0);
        wrapper.like(StrUtil.isNotBlank(keyword), PmsProduct::getName, keyword);
        wrapper.orderByDesc(PmsProduct::getSort, PmsProduct::getRecommandStatus);

        return productMapper.selectPage(page, wrapper);
    }

    @Override
    public List<PmsProduct> simpleList(String keyword) {
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProduct::getDeleteStatus, 0);
        wrapper.like(StrUtil.isNotBlank(keyword), PmsProduct::getProductSn, keyword);
        wrapper.like(StrUtil.isNotBlank(keyword), PmsProduct::getName, keyword);
        return productMapper.selectList(wrapper);
    }

    @Override
    public void updateDeleteStatus(Integer deleteStatus, List<Integer> ids) {
        List<PmsProduct> list = productMapper.selectBatchIds(ids);
        if (ObjectUtil.isNotEmpty(list)) {
            for (PmsProduct pmsProduct : list) {
                pmsProduct.setDeleteStatus(deleteStatus);
                productMapper.updateById(pmsProduct);
            }
        }
    }

    @Override
    public void updateNewStatus(Integer newStatus, List<Integer> ids) {
        List<PmsProduct> list = productMapper.selectBatchIds(ids);
        if (ObjectUtil.isNotEmpty(list)) {
            for (PmsProduct pmsProduct : list) {
                pmsProduct.setNewStatus(newStatus);
                productMapper.updateById(pmsProduct);
            }
        }
    }

    @Override
    public void updatePublishStatus(Integer publishStatus, List<Integer> ids) {
        List<PmsProduct> list = productMapper.selectBatchIds(ids);
        if (ObjectUtil.isNotEmpty(list)) {
            for (PmsProduct pmsProduct : list) {
                pmsProduct.setPublishStatus(publishStatus);
                productMapper.updateById(pmsProduct);
            }
        }
    }

    @Override
    public void updateReocmmandStatus(Integer recommendStatus, List<Integer> ids) {
        List<PmsProduct> list = productMapper.selectBatchIds(ids);
        if (ObjectUtil.isNotEmpty(list)) {
            for (PmsProduct pmsProduct : list) {
                pmsProduct.setRecommandStatus(recommendStatus);
                productMapper.updateById(pmsProduct);
            }
        }
    }

    @Override
    public void updateVerifyStatus(Integer verifyStatus, List<Integer> ids) {
        List<PmsProduct> list = productMapper.selectBatchIds(ids);
        if (ObjectUtil.isNotEmpty(list)) {
            for (PmsProduct pmsProduct : list) {
                pmsProduct.setVerifyStatus(verifyStatus);
                productMapper.updateById(pmsProduct);
            }
        }
    }

    @Override
    public PmsProduct create(PmsProduct pmsProduct) {
        PmsProduct product = productMapper.selectOne(new LambdaQueryWrapper<PmsProduct>().eq(PmsProduct::getProductSn, pmsProduct.getProductSn()));
        if (ObjectUtil.isNotEmpty(product)) {
            throw new BusinessException("货号已存在");
        }
        productMapper.insert(pmsProduct);
        return pmsProduct;
    }

    @Override
    public PmsProduct findById(Integer id) {
        return productMapper.selectById(id);
    }

    @Override
    public String updateById(PmsProduct pmsProduct) {
        PmsProduct one = productMapper.selectOne(new LambdaQueryWrapper<PmsProduct>().eq(PmsProduct::getProductSn, pmsProduct.getProductSn()));
        if (ObjectUtil.isNotEmpty(one)) {
            if (!one.getId().equals(pmsProduct.getId())) {
                return "商品货号已存在";
            }
        }
        productMapper.updateById(pmsProduct);
        return "ok";
    }
}
